﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Final
{
    class Scanner
    {
        string source;
        int position;
        List<Token> tokens;

        public Scanner(string input)
        {
            source = input;

            position = 0;
            tokens = new List<Token>();

            Read_In();
        }

        private void Read_In()
        {
            char c = source[position];

            if (c == ' ' || c == '\n' || c == '\r')
            {
                position++;
                Read_In();
            }
            else if (Char.IsDigit(c))
            {
                int temp = 0;

                while (Char.IsDigit(c))
                {
                    temp = 10 * temp + (int)Char.GetNumericValue(c);
                    position++;
                    c = source[position];
                }

                tokens.Add(new Token_Literal(temp));

                Read_In();
            }
            else if (c == '+' || c == '-' || c == '*' || c == '/')
            {
                tokens.Add(new Token_Operator(c));

                position++;

                Read_In();
            }
            else if (c == '<' || c == '>' || c == '=' || c == '_' || c == '←')
            {
                tokens.Add(new Token_Operator(c));

                position++;

                Read_In();
            }
            else if (c == ';' || c == ':' || c == '(' || c == ')')
            {
                tokens.Add(new Token_Operator(c));

                position++;

                Read_In();
            }
            else if (Char.IsLetter(c))
            {
                StringBuilder sb = new StringBuilder();

                while (c != ' ' && Char.IsLetter(c))
                {
                    sb.Append(c);
                    position++;
                    c = source[position];
                }

                string temp = sb.ToString();

                if (temp == "BEGIN")
                {
                    tokens.Add(new Token_Operator('0'));

                    Read_In();
                }
                else if (temp == "END")
                {
                    tokens.Add(new Token_Operator('1'));
                }
                else if (temp == "GOTO")
                {
                    tokens.Add(new Token_Operator('2'));

                    Read_In();
                }
                else if (temp == "IF")
                {
                    tokens.Add(new Token_Operator('3'));

                    Read_In();
                }
                else if (temp == "THEN")
                {
                    tokens.Add(new Token_Operator('4'));

                    Read_In();
                }
                else
                {
                    tokens.Add(new Token_Variable(temp));

                    Read_In();
                }
            }
            else
            {
                throw (new Exception("SCANNER: Invalid character '" + c + "' ecountered!"));
            }
        }

        public void PrintTokens()
        {
            foreach (Token token in tokens)
            {
                Console.WriteLine(token.GetType());
            }
        }

        public List<Token> Tokens
        {
            get { return tokens; }
        }
    }
}
